﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace SCS.Web.UI.WebControls.Dialogs
{
    public class TemplatedDialog : Dialog
    {
        private ITemplate _template;
        private TemplateOwner _templateOwner;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            EnsureChildControls();

            ITemplate temp = (_template != null) ? _template : new DefaultTemplate();
            temp.InstantiateIn(_templateOwner);
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            _templateOwner = new TemplateOwner();

            Controls.Add(_templateOwner);
        }

        protected override void RenderDialogContent(HtmlTextWriter writer)
        {
            base.RenderDialogContent(writer);

            _templateOwner.RenderControl(writer);
        }

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(typeof(ITemplate), "")]
        [Description("Dialog content"), TemplateContainer(typeof(TemplatedDialog))]
        public virtual ITemplate DialogContent
        {
            get
            {
                return _template;
            }
            set
            {
                EnsureChildControls();
                _template = value;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public TemplateOwner Template
        {
            get
            {
                return _templateOwner;
            }
        }
    }
}
